/*
 *  Copyright (C) 2010 Glauco
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.balisunrise.swing.transaction.view;

import java.awt.Component;
import java.awt.Frame;
import java.util.List;
import javax.swing.JDialog;
import javax.swing.WindowConstants;
import org.balisunrise.annotation.VisualAction;
import org.balisunrise.application.Application;
import org.balisunrise.builder.BuilderException;
import org.balisunrise.common.TransactionActions;
import org.balisunrise.common.events.TransactionModeChangedEvent;
import org.balisunrise.common.TransactionModeListener;
import org.balisunrise.common.VisualEntity;
import org.balisunrise.swing.transaction.Transaction;

/**
 *
 * @author Glauco
 */
public class TransactionDialog extends JDialog 
        implements DialogView, TransactionModeListener{

    Transaction transaction;

    public TransactionDialog(Transaction transaction) {
        super();
        this.transaction = transaction;
        this.transaction.getTransactionAction().addListener(this);
        init();
    }

    public TransactionDialog(Frame owner, boolean modal, Transaction transaction) {
        super(owner,modal);
        this.transaction = transaction;
        init();
    }

    @VisualAction(alias="",
        icon="BACK1",
        index=Integer.MAX_VALUE,
        text="Sair",
        toolTipText="Fechar a Tela")
    public void sair(){
        this.dispose();
    }

    private void init(){
        try{
            List<org.balisunrise.common.VisualAction> actions =
                    Application.get()
                    .getVisualEntityManager()
                    .getVisualActionBuilder().create(this, "");

            if(actions.size() > 0){
                org.balisunrise.common.VisualAction va = actions.get(0);
                TransactionActions ta = transaction.getTransactionAction();
                ta.addAction(va);
            }
        }catch(BuilderException bex){
            System.out.println("Falha ao inicial TransactionDialog!\n" +
                    bex.getMessage());
        }

        this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

        transaction.showSearch();
        this.getContentPane().add(transaction.getVisualRepresentation());
        this.setTitle(transaction.getTitle());

        this.pack();
        this.repaint();
    }

    public Transaction getTransaction() {
        return transaction;
    }

    public void setTransaction(Transaction transaction) {
        this.transaction = transaction;
    }

    public VisualEntity getVisualEntity() {
        return transaction;
    }

    public Boolean getConfirmation() {
        return null;
    }

    @Override
    public void setVisible(boolean value){
        setLocationRelativeTo((Component) Application.get().getVisualRepresentation());
        super.setVisible(value);
    }

    public void transactionModeChanged(TransactionModeChangedEvent evt) {
        pack();
    }

    public JDialog getVisualRepresentation() {
        return this;
    }
}
